package com.coder.oj.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.admin.mapstruct.CommitMapstruct;
import com.coder.oj.admin.mapstruct.UserMapstruct;
import com.coder.oj.admin.vo.CommitVo;
import com.coder.oj.common.core.BaseQuery;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.Commit;
import com.coder.oj.common.mapper.CommitMapper;
import com.coder.oj.common.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bosen 2022/11/28 11:50
 */
@Service
public class CommitService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommitMapper commitMapper;

    @Autowired
    private CommitMapstruct commitMapstruct;

    /**
     * 分页查询
     * @author Bosen 2022/12/16 10:00
     **/
    public JsonPage<CommitVo> getCommitPage(BaseQuery params) {
        LambdaQueryWrapper<Commit> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), Commit::getStatus, params.getStatus());
        IPage<Commit> page = commitMapper.selectPage(new Page<>(params.getPage(), params.getPageSize()), Wrappers.emptyWrapper());
        List<CommitVo> collect = page.getRecords().stream().map(commit -> {
            CommitVo vo = commitMapstruct.toVO(commit);
            vo.setCreateBy(userMapper.getUserNickname(commit.getCreateBy()));
            vo.setUpdateBy(userMapper.getUserNickname(commit.getUpdateBy()));
            return vo;
        }).collect(Collectors.toList());
        return new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
    }
}
